﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace StateManager
{
    class croc : log
    {
        bool isOpen = false;
        //l3
//clcl
//sstt
//LLLL
//SSSS
//stts
        //l1
//llll
//sttt
//LLLL
//SSSS
//ttts

        float timeWait;
        const float timeHeadOpen = 2000;
        Texture2D mouthOpen;
        Texture2D mouthClosed;

        public override Rectangle colRec
        {
            get
            {
                //return base.colRec;

                //if (isOpen)
                //{
                //    return new Rectangle((int)pos.X, (int)pos.Y, tex.Width, tex.Height);

                //}
                //else
                //    return new Rectangle((int)pos.X, (int)pos.Y, tex.Width + mouthClosed.Width, tex.Height);
                return new Rectangle((int)pos.X, (int)pos.Y, tex.Width + mouthOpen.Width, tex.Height);
            }
        }
        public Rectangle crocsHead
        {
            get
            {
                if (isOpen)
                {
                    if (dir == Direction.Left)
                        return new Rectangle((int)pos.X, (int)pos.Y, tex.Width - 80, tex.Height);
                    else
                        return new Rectangle((int)pos.X + 160, (int)pos.Y, mouthOpen.Width, mouthOpen.Height);
                }
                else
                    return Rectangle.Empty;
            }

        }
        public croc(Direction dir, Level lvl, float time, float x, float y, float velx, int pace)
            : base(dir, lvl, time, x, y, velx, pace)
        {
        }

        public override void loadContent(Level lvl)
        {
            //base.loadContent(lvl);
            tex = lvl.Content.Load<Texture2D>("images/c1");
            mouthOpen = lvl.Content.Load<Texture2D>("images/c3");
            mouthClosed = lvl.Content.Load<Texture2D>("images/c2");
        }

        public override void Update(GameTime gameTime, Rectangle viewPort)
        {
            base.Update(gameTime, viewPort);

            timeWait += gameTime.ElapsedGameTime.Milliseconds;

            if (timeWait >= timeHeadOpen)
            {
                isOpen = !isOpen;
                timeWait -= timeHeadOpen;
            }

        }

        public override void Draw(SpriteBatch spriteBatch, GameTime gameTime)
        {
            //base.Draw(spriteBatch, gameTime);
            Vector2 posInc = pos;

            spriteBatch.Draw(tex, posInc, Color.White);
            posInc.X += 160;

            if (isOpen)
                spriteBatch.Draw(mouthOpen, posInc, Color.White);
            else
                spriteBatch.Draw(mouthClosed, posInc, Color.White);
        }

        public override checkLocation HandleCollisions(Level lvl, Player player)
        {
            //Direction.
            //return base.HandleCollisions(lvl, player);
            Rectangle bounds = player.plyrRect;
            //int leftTile = (int)Math.Floor((float)bounds.Left / tile.width);
            //int rightTile = (int)Math.Ceiling(((float)bounds.Right / tile.width)) - 1;
            //int topTile = (int)Math.Floor((float)bounds.Top / tile.height);
            //int bottomTile = (int)Math.Ceiling(((float)bounds.Bottom / tile.height)) - 1;

            int playerX = (int)Math.Floor((float)player.Position.X / tile.width);
            int playerY = (int)Math.Ceiling((float)player.Position.Y / tile.height);
            // these cordinates are currently used to check what tile the player is on
            // i don't really understand what the math methods do

            // by using the playerx and playery variables, the enum variable check is assigned
            // what vis returned from the GetCollision method
            collisionCheck check = lvl.GetCollision(playerX, playerY);

            // if the player overlaps the log (calling it a truck for now)
            if (player.plyrRect.Intersects(colRec))
            {
                if (player.plyrRect.Intersects(crocsHead))
                    return checkLocation.crocbite;
                else
                    return checkLocation.onLog; // the method returns the enum checkLocation.onLog
            }
            //if (player.plyrRect.Intersects(colRec) && is
            else if (check != collisionCheck.passable) //else if the player is on a kill tile
            {
                //frogOn = false;
                return checkLocation.drowning; // then the drowning enum is returned (kills the player)
                //thing = checkLocation.drowning;
            }
            else // else the player is on ground
            {
                return checkLocation.onGround;
            }
        }
    }
}
